package com.gdcp.controller.portal;

import com.gdcp.beans.Shipping;
import com.gdcp.beans.User;
import com.gdcp.common.ResponseModel;
import com.gdcp.config.RequestHolder;
import com.gdcp.service.ShippingService;
import com.gdcp.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author 杨华京
 * @version 1.0
 * @description
 * @since 1.8
 */
@Controller
@RequestMapping("/render/shipping/")
public class ShippingController {

    @Autowired
    ResponseModel responseModel;
    @Autowired
    ShippingService shippingService;

    @RequestMapping(value = "/addressAdd",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addressAdd(){
        HttpServletRequest request=RequestHolder.getRequest();
        User user= (User) RequestHolder.getSession("user");
        Shipping shipping=getShipping(request,user);

        int count = shippingService.insert(shipping);
        if(count>0){
            //request.setAttribute("shipping",shipping);
            return responseModel.setModel(true,shipping);
        }

        return responseModel.setModel(false,"失败");
    }

    @RequestMapping(value = "/addressEdit",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addressEdit(){
        HttpServletRequest request=RequestHolder.getRequest();
        User user=(User)RequestHolder.getSession("user");
        Shipping shipping=getShipping(request,user);

        int count = shippingService.updateAddress(shipping);
        if(count>0){
            return responseModel.setModel(true,"成功");
        }

        return responseModel.setModel(false,"失败");
    }

    private Shipping getShipping(HttpServletRequest request,User user){
        Shipping shipping=new Shipping();
        if (HttpServletRequestUtil.getInt(request,"id")!=-1){
            shipping.setId(HttpServletRequestUtil.getInt(request,"id"));
        }

        shipping.setUser_id(user.getId());
        shipping.setReceiver_name(HttpServletRequestUtil.getString(request,"name"));
        shipping.setReceiver_province(HttpServletRequestUtil.getString(request,"province"));
        shipping.setReceiver_city(HttpServletRequestUtil.getString(request,"city"));
        shipping.setReceiver_address(HttpServletRequestUtil.getString(request,"area"));
        shipping.setReceiver_district(HttpServletRequestUtil.getString(request,"xiangxi"));
        shipping.setReceiver_zip(HttpServletRequestUtil.getString(request,"youbian"));
        shipping.setReceiver_phone(HttpServletRequestUtil.getString(request,"shipping"));
        shipping.setReceiver_mobile(HttpServletRequestUtil.getString(request,"dianhua"));
        return shipping;
    }


    @RequestMapping(value = "/addressList",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addressList(){
        User user=(User) RequestHolder.getSession("user");
        List<Shipping> shippings=shippingService.queryListById(user.getId());
        if (shippings!=null){
            return responseModel.setModel(true,shippings);
        }

        return responseModel.setModel(false,"失败");
    }

    @RequestMapping(value = "/address_id",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addressId(String shipping_id){
        if (shipping_id==null||shipping_id.equals("")){
            return responseModel.setModel(false,"地址id不能为空");
        }
        //User user=(User) RequestHolder.getSession("user");
        Shipping shipping=shippingService.getInformation(Integer.valueOf(shipping_id));
        if (shipping!=null){
            return responseModel.setModel(true,shipping);
        }

        return responseModel.setModel(false,"失败");
    }

    @RequestMapping(value = "/address",method = RequestMethod.GET)
    public String address(){
        HttpServletRequest request=RequestHolder.getRequest();
        User user=(User) RequestHolder.getSession("user");
        List<Shipping> shippings=shippingService.queryListById(user.getId());

        if (shippings!=null){
            request.setAttribute("shippings",shippings);
        }

        return "frontend/address";
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> delete(Integer id){
        int count=shippingService.deleteById(id);
        if (count<=0){
            return responseModel.setModel(false,"删除失败");
        }
        return responseModel.setModel(true,"删除成功");
    }
}